<?php
class CouponHistory extends AppModel {
    var $name = 'CouponHistory';

    public function usage_history ($id) {
        $sql  = 'SELECT c.`id`, c.`coupon_id`, c.`created`, s.`id`, s.`amount` FROM `coupon_histories` AS c ' .
                'INNER JOIN `sales` AS s ON c.`sale_id`=s.`id` WHERE c.`member_id`='.$id;
        $data = $this->query($sql);
        $resp = array();

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $u) {
            $resp[] = array(
                'id'        => $u['c']['coupon_id'],
                'ref'       => $u['s']['id'],
                'amount'    => number_format($u['s']['amount'], 2),
                'datetime'  => $this->serverToClientTime($u['c']['created'], 'd/m/Y H:ia'),
                'sortTime'  => $this->serverToClientTime($u['c']['created'], 'Y-m-d H:i:s'),
                'effective' => '',
                'type'      => __('Usage', true),
                'details'   => __('This line is the record for you to use your coupons', true)
            );
        }

        return $resp;
    } // usage_history
} // CouponHistory
?>